import { for_obj, d_sort_num, adjust_val, show_yn, is_empty, loc, show_mt, obj_str, d_sort, uJob, cline } from 'sui';
import { load_supply } from 'stock/base/cmd';

const show_seal = (v) => {
    if (v === "Y") {
        return loc("l_163");
    } else if (v === "S") {
        return loc("l_164");
    } else if (v === "N") {
        return loc("l_821");
    }

    return v;
};

const show_qyd = (v) => {
    if (v === "Y") {
        return loc("l_165");
    } else if (v === "N") {
        return loc("l_166");
    }

    return v;
};

const reload = (hDone, setMain, cur) => {
    const sn = obj_str(cur, "sn");

    uJob(hDone, "goods/listpsn",
        {
            line: cline(),
            k: sn,
        },
        r => {
            if (sn.endsWith("DT")) {
                d_sort(r, "groupsn");
            } else {
                d_sort(r, "sn");
            }

            r.forEach((d, i) => {
                adjust_val(d);
                d["index"] = i + 1;
            });

            setMain(r);
        }, e => setMain(null));
};

const load_goods = (hDone, setMain) => {
    uJob(hDone, "goods/list", { line: cline() },
        r => {
            d_sort(r, "sn");

            r.forEach((d, i) => d["index"] = i + 1);

            setMain(r);
        },
        e => setMain(null));
};

const is_pair = (d) => {
    const sn = obj_str(d, "sn");
    if (sn.endsWith("DT")) {
        return true;
    }

    return false;
};

const get_col = (col, cur, mapLub) => {
    if (is_empty(col)) {
        return null;
    }

    const tmp = [
        {name: loc("l_193"), key: 'index', width: 70, frozen: true},
        {name: loc("l_851"), key: 'boxgood', width: 110},
        {name: loc("l_529"), key: 'seal', width: 100, formatter: props => show_seal(obj_str(props.row, "seal")) },
        {name: loc("l_868"), key: 'lub', width: 100, formatter: props => {
            const d = props.row;
            return obj_str(mapLub, d.lub);
        }},
        {name: loc("l_161"), key: 'mfc', width: 100, formatter: props => show_yn(obj_str(props.row, "mfc")) },
        {name: loc("l_162"), key: 'qyd', width: 100, formatter: props => show_qyd(obj_str(props.row, "qyd")) },
    ];

    if (is_pair(cur)) {
        tmp.push({name: loc("l_751"), key: 'groupsn', width: 110});
        tmp.push({name: loc("l_229"), key: 'gtp', width: 110});
        tmp.push({name: loc("l_869"), key: 'other', width: 110});
    }

    col.forEach(d => {
        const sn = obj_str(d, "sn");

        if (sn.startsWith("box")
            || sn === "groupsn" || sn === "gtp" || sn === "other"
            || sn === "seal" || sn === "lub") {
            return;
        }

        show_mt(tmp, d, 110);
    });

    return tmp;
};

const load_lub = (mapSub) => load_supply("lub", null, mapSub, "stuff", "gstuff");

const sum_standout = (val, setSum) => {
    const sum_map = {};

    val.forEach(d => {
        const b = obj_str(d, "standout");

        if (sum_map[b]) {
            sum_map[b] += 1;
        } else {
            sum_map[b] = 1;
        }
    });

    const sum_row = []

    for_obj(sum_map, (k, v) => sum_row.push({ sn: parseInt(k, 10), val: v }));

    d_sort_num(sum_row, "sn");

    setSum(sum_row);
};

export {sum_standout, load_lub, get_col, is_pair, load_goods, reload};
